package com.simple.redissonrpc.core;

import cn.hutool.extra.spring.SpringUtil;
import com.simple.redissonrpc.config.GameConfig;
import java.io.Serializable;
import java.util.concurrent.atomic.AtomicLong;
import lombok.Data;

@Data
public class ServerInternalMessage<T extends IRpcInternalMsg> implements Serializable {

    private static final long serialVersionUID = 1L;

    // 唯一消息号生成器，主要是：rpc消息返回时能返回到正确的回调函数
    private static AtomicLong seqIdGenerator = new AtomicLong();

    // 生成的消息标识
    private long seqId = seqIdGenerator.incrementAndGet();

    // 从哪个服务器发来的消息
    private int fromServerId;

    // 消息 参考:RpcMsgType
    private int type;

    // RpcMsgType.CALL消息(不区分是同步还是异步)，这个才有值,用于返回数据
    private long callbackId;

    // 消息标识
    private String className;

    private T data;

    private long machineId;

    public static <T extends IRpcInternalMsg> ServerInternalMessage<T> valueOf(int fromServerId, String className, T data, int type) {
        ServerInternalMessage<T> message = new ServerInternalMessage<>();
        message.setFromServerId(fromServerId);
        message.setClassName(className);
        message.setData(data);
        message.setType(type);
        message.setMachineId(SpringUtil.getBean(GameConfig.class).getMachineId());
        return message;
    }
}
